/**
 * Created by no1 on 2015/12/21.
 */
'use strict';
let DD = require('./daoLoader');

let F = require('../function')
let Base = require('./Base');
let _ = require('underscore');
_.str = require('underscore.string');
_.v = require('validator');

let util = require("util");
let CouponMongoDao = new DD.mongoDB.CouponDao();
function CouponDispatcher(data) {
  data = data || {};
  data.dao = data.dao || CouponMongoDao;
  data.esDao = data.esDao || {};
  data.redisDao = data.redisDao || {};
  data.modelName = data.modelName || 'coupon';
  data.modelType = data.modelType || 'mongo';
  this.data = data;
  Base.call(this, this.data);
}
util.inherits(CouponDispatcher, Base);
let DP = CouponDispatcher.prototype;
/**
 * @todo 添加
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.add = function*(fields) {
  if (!fields.name)return F.returnData(1, '必须有名字');
  if (!fields.vendorid)return F.returnData(1, '必须有商家id');
  if (!fields.type)return F.returnData(1, '必须指定优惠券类型');
  let parameters = {};
  parameters.name = fields.name;
  parameters.vendorid = fields.vendorid;
  parameters.money = fields.money;
  parameters.circulation = fields.circulation;
  parameters.applicableGte = fields.applicableGte;
  parameters.applicableUser = fields.applicableUser;
  parameters.onLineTime = fields.onLineTime;
  parameters.offLineTtime = fields.offLineTtime;
  parameters.type = fields.type;

  parameters.status = 1;
  parameters.valid = 1;
  parameters.provide = 0;
  parameters.couponCode = 'YN' + _.random(10000000, 99999999);
  //保存
  let ret = yield this.dao.findAndSave({id: 0}, parameters);
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.data);
  return F.returnData(0, ret.msg, ret.data);
};
/**
 * todo 更新
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.update = function*(fields) {
  if (!fields.id)return F.returnData(1, '必须有优惠券id');
  let parameters = {}, arr = ['name', 'money', 'circulation', 'applicableGte', 'applicableUser', 'onLineTime',
    'offLineTtime', 'type', 'status', 'valid', 'provide']
  for (let one of arr) if (fields[one] != null) parameters[one] = fields[one];
  parameters.updateAt = new Date();
  //更新数据
  let ret = yield this.dao.findAndUpdate({id: fields.id}, parameters, false);
  if (ret.error == 1) return F.returnData(1, ret.msg);

  let json = JSON.parse(JSON.stringify({data: ret.data}));
  let retData = F.dataFilter(json, ['_id', 'createdAt', 'updateAt', '__v', '__t']);
  return F.returnData(0, ret.msg, retData);
};
/**
 * todo 列表
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.list = function*(fields) {
  let parameters = {}, opt = {}, isSort = {id: -1}, retDataList, retData = {}, retConut, ret
    , page = fields.page || 1
    , pageSize = fields.pageSize || 50
    , arr = ['id', 'name', 'status'];
  //默认类别
  if (fields.act == 1) {
    for (let one of arr) opt[one] = 1;
  }
  //后台优惠券管理
  else if (fields.act == 2) {
    if (!fields.type) return F.returnData(1, '必须指定优惠券类别');
    if (!fields.vendorid) return F.returnData(1, '必须指定商家id');
    arr = arr.concat(['money', 'circulation', 'receivingQuantity', 'applicableUser', 'onLineTime', 'offLineTtime', 'type', 'provide', '']);
    let offLineTtime = fields.offLineTtime, onLineTime = fields.onLineTime;
    for (let one of arr) opt[one] = 1;
    parameters.type = fields.type;
    parameters.vendorid = fields.vendorid;
    if (!!fields.query) {
      fields.query || (fields.query = "");
      let str = fields.query.replace(/[\+]*[\*]*[\?]*[\$]*[\^]*[\\]*/g, '');
      str = str.split('').join('+[\\s\\S]*');
      let re = new RegExp(str, 'i');
      parameters.name = {$regex: re};
    }

    if (!!onLineTime) parameters.onLineTime = {$gte: onLineTime};
    if (!!offLineTtime) parameters.offLineTtime = {$lt: offLineTtime};
  }

  //获取列表
  ret = yield this.dao.getByQueryPor(parameters, opt, {page: page, pageSize: pageSize}, isSort);
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.data);
  retDataList = ret.data;

  let json = JSON.parse(JSON.stringify(retDataList));
  retDataList = F.dataFilter(json, ['_id']);

  if (fields.isPaging == 1) retConut = yield this.dao.getCountByQuery(parameters);
  if (fields.isPaging == 1) {
    retData.list = retDataList;
    retData.count = retConut.data;
  } else retData = retDataList;

  return F.returnData(0, 'ok', retData);
};
/**
 * todo 详情
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.detail = function*(fields) {
  if (!fields.id) return F.returnData(1, '必须指定id');
  let ret, retData
    , parameters = {}, opt = {}
    , arr = ['id', 'name', 'couponCode', 'money', 'circulation', 'receivingQuantity', 'applicableGte', 'applicableUser'
      , 'onLineTime', 'offLineTtime', 'type', 'provide', 'valid', 'status']
    , arrDel = ['_id', '__v', '__t']
  //默认详情
  if (fields.act == 1) {
    for (let one of arrDel) opt[one] = 0;
    parameters.id = fields.id;
  }
  ret = yield this.dao.getByQuery(parameters, opt);
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.data);
  //retData = F.dataFilter(JSON.parse(JSON.stringify(ret.data)), ['_id', 'createdAt', 'updateAt', '__v', '__t']);

  return F.returnData(0, 'ok', ret.data);
};

/**
 * 内部调用
 * @param Qut
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.findByDis = function*(Que) {
  let arr = ['_id', '__v']
    , opt = {};
  for (let one of arr) opt[one] = 0;
  let ret = yield  this.dao.getByQuery(Que, opt);
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.data);
  return F.returnData(0, ret.msg, ret.data);
};

module.exports = CouponDispatcher;